package com.example.servlet;

import com.example.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/system-setting")
public class SystemSettingServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        listSettings(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String action = request.getParameter("action");
        
        if ("update".equals(action)) {
            updateSettings(request, response);
        } else {
            response.sendRedirect("system-setting");
        }
    }
    
    private void listSettings(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT * FROM system_settings ORDER BY setting_key";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            
            List<Map<String, Object>> settings = new ArrayList<>();
            while (rs.next()) {
                Map<String, Object> setting = new HashMap<>();
                setting.put("key", rs.getString("setting_key"));
                setting.put("value", rs.getString("setting_value"));
                setting.put("description", rs.getString("description"));
                settings.add(setting);
            }
            
            request.setAttribute("settings", settings);
            request.getRequestDispatcher("admin-system-setting.jsp").forward(request, response);
            
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "获取系统设置失败: " + e.getMessage());
            request.getRequestDispatcher("admin-system-setting.jsp").forward(request, response);
        }
    }
    
    private void updateSettings(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try (Connection conn = DBUtil.getConnection()) {
            // 获取所有设置参数
            String[] keys = request.getParameterValues("setting_key");
            String[] values = request.getParameterValues("setting_value");
            
            if (keys != null && values != null && keys.length == values.length) {
                for (int i = 0; i < keys.length; i++) {
                    String key = keys[i];
                    String value = values[i];
                    
                    // 更新设置
                    String sql = "UPDATE system_settings SET setting_value = ? WHERE setting_key = ?";
                    PreparedStatement stmt = conn.prepareStatement(sql);
                    stmt.setString(1, value);
                    stmt.setString(2, key);
                    stmt.executeUpdate();
                }
                
                request.setAttribute("success", "系统设置更新成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "更新系统设置失败: " + e.getMessage());
        }
        
        response.sendRedirect("system-setting");
    }
    
    // 获取单个设置值的工具方法
    public static String getSetting(String key) {
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "SELECT setting_value FROM system_settings WHERE setting_key = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, key);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                return rs.getString("setting_value");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    // 检查维护模式的工具方法
    public static boolean isMaintenanceMode() {
        String maintenanceMode = getSetting("maintenance_mode");
        return "true".equals(maintenanceMode);
    }
    
    // 检查功能是否启用的工具方法
    public static boolean isFeatureEnabled(String feature) {
        String enabled = getSetting("enable_" + feature);
        return "true".equals(enabled);
    }
} 